9.1. User-Defined Monad System Function

As an example of a monad function, let’s consider one that uses a tuple structure.
For example, let’s define an array containing information about a group of people and consider a function that indicates whether any of them can drive if even one person has a driver’s license.

PEOPLE: ⟨⟨κ[?], υ3⟩, β⟩[?] ← ⟨⟨"Taro", 174⟩, ⊥⟩ ⟨⟨"Hanako", 158⟩, ⊤⟩;

HAS_DRIVER: τ{τ{κ[?], υ3}, β} → β ← { 
    ⟨BASEINFO, LICENSE⟩ ← ω; 
    LICENSE 
};

⎕ ← IF@κ[?] (∨/ ⎕HAS_DRIVER¯people, "CAN DRIVE", CANNOT DRIVE);
"CAN DRIVE"

9.2. User-Defined Dyad System Function

As examples of dyad functions, let’s implement the mathematical permutation function \(nPr\) and combination function \(nCr\):

NPR: υ4, υ4 → υ4 ← { (×/1+ια) ÷ (×/1+ια-ω) };
NCR: υ4, υ4 → υ4 ← { (×/1+ια) ÷ ((×/1+ιω)×(×/1+ια-ω)) };

A ← 5;
B ← 3;
⎕ ← A ⎕NPR B;
⎕ ← A ⎕NCR B;
60
10

9.3. User-Defined Nilad System Function

This is a very important point to note, we cannot define custom nilad functions. To reiterate, please recall why nilad functions are necessary. They were actually classified as special values rather than functions. Generally speaking, we expect a function to produce different output values depending on the input values. Of course, even with a function that accepts inputs, if you use a nilad function internally, you can create a function that does not necessarily produce a deterministically unique value. However, that property itself is guaranteed by the nature of a function. Therefore, that point is not an issue. However, creating a function specifically for that purpose goes against the very purpose of creating functions.

9.4. Return-if System Output

The ⎕RETURNIF system output is special because it can only be used within user-defined named functions.
While many languages use a process like if → then → return, defining this all at once allows you to omit redundant syntax. It also eliminates the need for else statement.

While more specialized cases can be expressed using the ⎕IF function or the ⎕SWITCH function, this ⎕RETURNIF system output is all you need for conditional branching.

In this exercise, let’s create a function that allows people with a driver’s license to calculate the distance from their starting point to their destination. For those without a driver’s license, the function returns 0.

GETDIST: ⟨β, φ[2], φ[2]⟩ → φ ← {
    ⟨DL, SRC, DST⟩ ← ω;
    ⎕RETURNIF ← ⟨¬DL, 0.⟩;
    (%SRC[0]-DST[0])+(%SRC[1]-DST[1])
};

DL ← ⎕:β;
SRC ← ⎕:φ[2];
DST ← ⎕:φ[2];
⎕ ← ⎕GETDIST ⟨DL, SRC, DST⟩;
INPUT(β): 0
INPUT(φ[2]): 23.84 9.56
INPUT(φ[2]): 389.24 83.98
0.
INPUT(β): 0
INPUT(φ[2]): 23.84 9.56
INPUT(φ[2]): 389.24 83.98
439.82

9.5. Recursive Function

The ∇ (recursive) function is special glyph function that can only be used within user-defined named functions.
A recursive function calls itself again.
The most elegant mathematical example of a recursive function is the Euclidean algorithm:

GCD: υ4, υ4 → υ4 ← { IF:υ4 ⟨(α % ω) = 0, ω, ω ∇ α % ω⟩ };

⎕ ← 8125 ⎕GCD 647;
1

9.6. Anonymous Function

If we want to express a function in a single line and reuse the same value in multiple places, it is convenient to use an anonymous function.
Anonymous functions serve the same purpose as named functions, but they are distinct in that Anonymous functions can be nested within other functions.
Anonymous functions cannot be typed because it is typed automatically. Also, anonymous functions cannot be used for requirements that necessitate them.

⎕ ← 8125 { IF:υ4 ⟨(α % ω) = 0, ω, ω ∇ α % ω⟩ } 647;
1

Furthermore, since the use of arguments is essential, this cannot be applied to nilad functions. And there’s no need to do so.

⎕ ← 10 × ⎕RAND;
7.1503138532